home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.cyberramp.net!news
- From: sinan@cyberramp.net (John Noland)
- Newsgroups: comp.lang.c
- Subject: Re: Do you have ever pass structures?
- Date: 22 Feb 1996 04:38:51 GMT
- Organization: Prose Software
- Message-ID: <4ggs0r$2bt@newshost.cyberramp.net>
- References: <4ge8mi$qjm@srvr1.engin.umich.edu>
- NNTP-Posting-Host: ramp1-27.cyberramp.net
- X-Newsreader: WinVN 0.99.5
-
- In article <4ge8mi$qjm@srvr1.engin.umich.edu>, hasdi@news-server.engin.umich.edu says...
- >
- >snip...
- >So is there any REAL advantage is passing an entire structure? Do people
- >ever do it? I've some people's source code and almost always, they
- >pass pointers to structures instead of the structure itself. In a way,
- >passing a pointer to an array instead of the array itself might be
- >feature not a bug in C. :)
-
- If you look at the original K&R, the only things you could do with a
- structure were take its address with &, and access one of it's members.
- The "C Reference Manual"[RefMan81 - Bell Labs] introduced new operations
- on strucures and unions. It allowed them to, among other things, be passed
- as parameters and return types. These days the guidelines you should follow
- are laid out in the ANSI C standard and K&R, 2nd edition. If you don't have
- the 2nd edition of K&R. Get it. I would recommend you read it a few times
- and then keep it close at hand when you're programming. I know this doesn't
- really answer your question directly, but if you're going to be a good
- programmer you have to read a lot of books. If you're going to be a good
- C programmer, it helps if one of those books is K&R.
-
- Anyway, I'll give you this much (but you should still read K&R), C passes
- all arguments except arrays by value - a copy of a variable is passed to
- a function on the stack. (You could argue - correctly - that arrays are
- not really an exception because C passes the pointer to the first element
- by value, but that isn't the way it's usually presented). If you have a
- structure of any size, do you want it passed in it's entirety on the
- stack? Or would you prefer just a pointer on the stack? It's entirely
- to your discretion. For what it's worth, in my experience, I have seldom
- run across a circumstance where I have felt the need to pass a structure
- instead of a pointer.
-
- - John
-
-
-